package com.hzyb.shopEvaluation.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.hzyb.oldpack.dao.JdbcDao;
import com.hzyb.oldpack.service.ShopFileService;
import com.hzyb.shopEvaluation.dao.ShopEvaluationDao;
import com.hzyb.shopEvaluation.model.ShopEvaluation;
import com.hzyb.shopEvaluation.model.SubmitComment;
import com.hzyb.shopEvaluation.service.ShopEvaluationService;
import com.hzyb.shopOrder.dao.ShopOrderDao;
import com.hzyb.shopOrder.model.ShopOrder;
import com.hzyb.shopPictureTable.dao.ShopPictureTableDao;
import com.hzyb.shopPictureTable.model.ShopPictureTable;
@Service
public class ShopEvaluationServiceImpl implements ShopEvaluationService{

	@Resource
	private ShopEvaluationDao shopEvaluationDao;

	@Resource
	private JdbcDao JdbcDao;

	@Autowired
	private ShopOrderDao ShopOrderDao;

	@Autowired
	private ShopFileService ShopFileService;

	@Autowired
	private ShopPictureTableDao ShopPictureTableDao;

	@Override
	public Map<String, Object> getShopEvaluationByCommoditySid(String commoditySid) {
		Map<String, Object> map=new HashMap<>();
		map.put("totals",shopEvaluationDao.getTotalShopEvaluationByCommoditySid(commoditySid));
		//map.put("reputation",shopEvaluationDao.getReputationByCommoditySid(commoditySid));
		map.put("praiseNumber", shopEvaluationDao.findByCommoditySidAndGrade(commoditySid, "1").size());
		List<ShopEvaluation> list=shopEvaluationDao.findByCommoditySid(commoditySid);
		for(ShopEvaluation shopEvaluation:list) {
			shopEvaluation.setImgs(JdbcDao.getShopPictureTableByBusinessSid(shopEvaluation.getEvaluationSid()));
		}
		map.put("data", list);
		return map;
	}

	@Override
	public void submitComment(SubmitComment submitComment) throws Exception {
		final String FILETYPE="shopEvaluation";
		ShopEvaluation shopEvaluation=new ShopEvaluation();
		ShopOrder shopOrder=ShopOrderDao.findOne(submitComment.getOrder_id());
		if(shopOrder==null) {
			throw new Exception("order is not valid!!");
		}
		shopEvaluation.setCommoditySid(shopOrder.getCommoditySid());
		shopEvaluation.setContent(submitComment.getContent());
		shopEvaluation.setEnterDate(new Date());
		shopEvaluation.setEnterUserid(submitComment.getOpen_id());
		shopEvaluation.setGrade(submitComment.getGrade());
		shopEvaluation.setOederId(submitComment.getOrder_id());
		shopEvaluationDao.save(shopEvaluation);
		List<ShopPictureTable> shopPictureTables=new ArrayList<>();
		for(MultipartFile multipartFile:submitComment.getImages()) {
			if(!multipartFile.isEmpty()) {
				String filename=multipartFile.getOriginalFilename();
				String suffix=filename.substring(filename.lastIndexOf("."), filename.length());	
				filename=filename.substring(0, filename.lastIndexOf("."));
				Map<String, Object> map= ShopFileService.uploadFile(multipartFile.getBytes(), filename, suffix, FILETYPE);
				ShopPictureTable shopPictureTable=new ShopPictureTable();
				shopPictureTable.setBusinessSid(shopEvaluation.getEvaluationSid());
				shopPictureTable.setEnterDate(new Date());
				shopPictureTable.setEnterUserId(shopEvaluation.getEnterUserid());
				shopPictureTable.setFileId(map.get("file_id").toString());
				shopPictureTables.add(shopPictureTable);
			}
		}
		ShopPictureTableDao.save(shopPictureTables);	
	}

}
